package com.oig.sys.security.support;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.oig.common.exception.ServiceException;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.oauth2.core.OAuth2AuthenticatedPrincipal;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


@JsonInclude(JsonInclude.Include.NON_NULL)
public class SecurityUser extends User implements OAuth2AuthenticatedPrincipal {
    //"主键id"
    @Getter
    private Integer id;
    //工号
    @Getter
    private String code;
    //"别名"
    @Getter
    private String nickName;
    //"真实姓名，中文名"
    @Getter
    private String realName;

    @Getter
    private Integer defaultSysId;
    @Getter
    private String defaultSysName;
    @Getter
    private String defaultSysImgUrl;
    @Getter
    private Integer tenantId;
    @Getter
    private String tenantCode;
    @Getter
    private String tenantName;

    public SecurityUser(Integer id, String code, String nickName,String realName, String username, String password,
                        Integer defaultSysId,String defaultSysName, String defaultSysImgUrl,
                        Integer tenantId,String tenantCode,String tenantName,
                        Collection<? extends GrantedAuthority> authorities) {
        super(username, password, authorities);
        this.id = id;
        this.code = code;
        this.nickName = nickName;
        this.realName = realName;
        this.defaultSysId = defaultSysId;
        this.defaultSysName = defaultSysName;
        this.defaultSysImgUrl = defaultSysImgUrl;
        this.tenantId = tenantId;
        this.tenantCode = tenantCode;
        this.tenantName = tenantName;
    }

    public SecurityUser(Integer id, String code, String nickName, String realName, String username, String password,
                        Integer defaultSysId, String defaultSysName, String defaultSysImgUrl,
                        Integer tenantId,String tenantCode,String tenantName,
                        boolean enabled, boolean accountNonExpired,
                        boolean credentialsNonExpired, boolean accountNonLocked,
                        Collection<? extends GrantedAuthority> authorities) {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired,accountNonLocked, authorities);
        this.id = id;
        this.code = code;
        this.nickName = nickName;
        this.realName = realName;
        this.defaultSysId = defaultSysId;
        this.defaultSysName = defaultSysName;
        this.defaultSysImgUrl = defaultSysImgUrl;
        this.tenantId = tenantId;
        this.tenantCode = tenantCode;
        this.tenantName = tenantName;
    }

    /**
     * 校验租户权限
     */
    @JsonIgnore
    public void checkTenantPermission(Integer tenantId){
        if (this.tenantId==0) return;
        if (tenantId==null || !tenantId.equals(this.tenantId)){
            throw new ServiceException("无租户数据权限") ;
        }
    }





    @JsonIgnore
    @Override
    public Map<String, Object> getAttributes() {
        return new HashMap<>();
    }



    @JsonIgnore
    @Override
    public String getName() {
        return getUsername();
    }
}
